package com.swufestu.second;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class RateTask1 implements Runnable{
    private static final String TAG = "Task";
    Handler handler;

    public RateTask1(Handler handler){
        this.handler=handler;
    }
    List<String> retlist=new ArrayList<String>();
    public void run() {
        //List<String> retlist = new ArrayList<>();
        ArrayList listItems = new ArrayList<HashMap<String,String>>();
        Document doc = null;
        try {
            for(int i = 0;i<10;i++){
                if (i==0){
                    doc = Jsoup.connect("https://www.boc.cn/sourcedb/whpj/"+"index.html").get();
                }else{
                    doc = Jsoup.connect("https://www.boc.cn/sourcedb/whpj/"+"index_"+i+".html").get();
                }
                Log.i("xx", "run: title :"+doc.title());
                Elements tables = doc.getElementsByTag("table");
                Element table1 = tables.get(1);
                Elements trs = table1.getElementsByTag("tr");
                trs.remove(0);

                for(Element tr:trs){
                    HashMap<String,String> map = new HashMap<String,String>();
                    Elements tds = tr.getElementsByTag("td");
                    String cname = tds.get(0).text();
                    String cval = tds.get(5).text();
                    //retlist.add(cname+"--->"+cval);
                    map.put("country",cname);
                    map.put("rate",cval);
                    listItems.add(map);
                    //Log.i("xx", "run: cname"+cname+"---->"+"cval:"+cval  );
                }
            }
            //Log.i(TAG, "run: list:"+listItems);

            Message msg = handler.obtainMessage(9);
            msg.obj = listItems;
            handler.sendMessage(msg);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}